package com.tycoin.app.controller;

import com.tycoin.account.slaveentity.TycoinAccount;
import com.tycoin.account.slaveservice.TycoinAccountService;
import com.tycoin.app.response.BaseRespose;
import com.tycoin.user.slaveentity.TycoinUser;
import com.tycoin.user.slaveservice.TycoinUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by happy on 2017/8/21.
 */
@RestController
@RequestMapping(value = { "/trade" })
public class TradeController   extends AbstractController{
    @Autowired
    BaseRespose baseRespose;
    @Autowired
    private TycoinUserService tycoinUserService;
    @Autowired
    private TycoinAccountService tycoinAccountService;
    
    
    //设置内置账户  60000001



    public boolean checkSession(HttpServletRequest request){
        String userId = String.valueOf(request.getSession().getAttribute("userId"));
        TycoinUser user=tycoinUserService.findById(Long.valueOf(userId));
        if (user==null)
        {
            baseRespose.setCode(4008);
            baseRespose.setMassage("seesion错误");
            return true;
        }
        return false;
    }

    /**********
     * 市价卖出
     * @return
     */
    public BaseRespose sellNow(HttpServletRequest request, HttpServletResponse response){
        if (checkSession(request))
            return baseRespose;
        String userId = String.valueOf(request.getSession().getAttribute("userId"));
        TycoinAccount tycoinAccount=tycoinAccountService.findOneByuserId(userId);
        String coinType=request.getParameter("coinType");

        //todo 扣款等等

        return baseRespose;
    }

    /********
     * 市价买入
     * @return
     */
    public BaseRespose buyNow(HttpServletRequest request, HttpServletResponse response){
        if (checkSession(request))
            return baseRespose;
        String userId = String.valueOf(request.getSession().getAttribute("userId"));
        TycoinAccount tycoinAccount=tycoinAccountService.findOneByuserId(userId);
        String coinType=request.getParameter("coinType");
        //实时价格
        String moneyNow=request.getParameter("moneyNow");
        //买入个数
        String payMum=request.getParameter("payMum");
        //挂单扣款
        
        
        
        
         return baseRespose;
    }

    /******
     * 挂单卖出
     * @return
     */
    public BaseRespose sellBook(HttpServletRequest request, HttpServletResponse response){
        if (checkSession(request))
            return baseRespose;
        String userId = String.valueOf(request.getSession().getAttribute("userId"));
        TycoinAccount tycoinAccount=tycoinAccountService.findOneByuserId(userId);
        String coinType=request.getParameter("coinType");

        //todo 扣款等等
        return baseRespose;
    }

    /**
     * 挂单买入
     * @return
     */
    public BaseRespose buyBook(HttpServletRequest request, HttpServletResponse response){
        if (checkSession(request))
            return baseRespose;
        String userId = String.valueOf(request.getSession().getAttribute("userId"));
        TycoinAccount tycoinAccount=tycoinAccountService.findOneByuserId(userId);
        String coinType=request.getParameter("coinType");

        //todo 扣款等等
        return baseRespose;
    }
}
